package com.example.controller;

import com.example.entity.Position;
import com.example.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author shenxiang
 * @date 2020/11/28 10:57
 * @description
 */
@Controller
public class PositionController {

    @Autowired
    private PositionService positionService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<Map<String,Object>> search(@PathVariable("keyword") String keyword,
                            @PathVariable("pageNo") int pageNo,
                            @PathVariable("pageSize") int pageSize) throws IOException {
        List<Map<String, Object>> list = positionService.searchPos(keyword,"positionName", pageNo, pageSize);
        // 如果根据关键词查询出来的数量不足5条,则使用 美女多、员工福利好进行查询
        if (list.size() < 5){
            list.addAll(positionService.searchPos("美女多、员工福利好","positionAdvantage",1,5-list.size()));
        }
        return list;
    }

    @RequestMapping("/importAll")
    @ResponseBody
    public String importAll(){
        positionService.importAll();
        return "success";
    }
}
